
package com.example.dynamicschedule.service.impl;

import com.example.dynamicschedule.bean.ScheduleJobLog;
import com.example.dynamicschedule.bean.ScheduleJobLogExample;
import com.example.dynamicschedule.dao.ScheduleJobLogMapper;
import com.example.dynamicschedule.service.ScheduleJobLogService;
import com.example.dynamicschedule.utils.FileDownloadUtil;
import com.example.dynamicschedule.utils.ReadPathFiles;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("scheduleJobLogService")
public class ScheduleJobLogServiceImpl implements ScheduleJobLogService {


	@Autowired
	private ScheduleJobLogMapper scheduleJobLogMapper;
	protected static final Logger logger = LoggerFactory.getLogger(ScheduleJobLogServiceImpl.class);
	@Override
	public PageInfo queryPage(Map<String, Object> params) {
		try {
			logger.info("获取joblog列表start---参数：param:"+params.toString());
			Object jobIdObject = params.get("jobId");
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Object startTime = params.get("startTime");
			Object endTime = params.get("endTime");
			Object param = params.get("param");
			Object pageObject = params.get("page");
			Object pageSizeObject = params.get("pageSize");
			Object status = params.get("status");
			Integer page=0;
			Integer pageSize=10;
			if (pageObject != null && pageSizeObject != null) {
				page = Integer.parseInt(String.valueOf(pageObject));
				pageSize= Integer.parseInt(String.valueOf(pageSizeObject));
			}
			PageHelper.startPage(page, pageSize);
			ScheduleJobLogExample scheduleJobLogExample = new ScheduleJobLogExample();
			ScheduleJobLogExample.Criteria criteria = scheduleJobLogExample.createCriteria();
			if (jobIdObject!=null) {
				Long jobId = Long.parseLong(String.valueOf(jobIdObject));
				criteria.andJobIdEqualTo(jobId);
			}
			if (startTime!=null&&endTime!=null) {
				Date startTimeDate = simpleDateFormat.parse(String.valueOf(startTime));
				Date endTimeDate = simpleDateFormat.parse(String.valueOf(endTime));
				criteria.andCreateTimeBetween(startTimeDate,endTimeDate);
			}
			if (param!=null) {
				criteria.andParamsLike("%"+String.valueOf(param)+"%");
			}
			if (status != null) {
				criteria.andStatusEqualTo(Byte.valueOf(String.valueOf(status)));
			}
			List<ScheduleJobLog> scheduleJobLogs = scheduleJobLogMapper.selectByExample(scheduleJobLogExample);
			PageInfo pageInfo = new PageInfo<>(scheduleJobLogs);
			logger.info("获取joblog列表end---result：pageInfo:"+pageInfo.toString());
			return pageInfo;
		} catch (Exception e) {
			logger.error("获取joblog列表出错异常"+e.getMessage());

			return null;
		}
	}

	@Override
	public Map<String, Object> fileUploadOss() {
		Map<String,Object> result=new HashMap<>();
		 //读取文件夹下的文件
		 File file = ReadPathFiles.readFileByPath("");
		 FileDownloadUtil fileDownloadUtil=new FileDownloadUtil();
		if(null == file ){
			result.put("result","file is null");
			  return result;
		}
		//传入key
		result=fileDownloadUtil.uploadOSSA("",file);
		return result;
	}

}
